package synchronicity.host;

import java.io.IOException;

import synchronicity.calendar.Calendar;
import synchronicity.discovery.BluetoothDiscovery;

/**
 * A SchedulerHostFactory for creating SchedulerHosts that use bluetooth
 * for discovery/communications
 */
public class BluetoothSchedulerHostFactory implements SchedulerHostFactory {

	private static BluetoothSchedulerHostFactory instance;

	private BluetoothSchedulerHostFactory() {

	}

	public static synchronized BluetoothSchedulerHostFactory getInstance() {
		if (instance == null) {
			instance = new BluetoothSchedulerHostFactory();
		}
		return instance;
	}

	public SchedulerHost createSchedulerHost(Calendar calendar) {
		try {
			SchedulerHostImpl result = new SchedulerHostImpl(calendar,
					BluetoothDiscovery.getInstance());
			// Skeleton starts off its own thread of execution that keeps it
			// in memory and working.
			SchedulerHostSkeleton skeleton = new SchedulerHostSkeleton(result);
			result.setHostIdentifier(skeleton.getHostIdentifier());
			return result;
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
}
